/**
 * 
 */
package cgl.narada.util;

import java.util.Properties;

import org.apache.log4j.Logger;

import cgl.narada.service.security.securityprovider.CertificateManager;
import cgl.narada.service.security.securityprovider.KeyToolFacade;

/**
 * Generates Secure keys and certificates for SSL connection to broker. This is
 * stored statically in <code>NB_HOME/keystore/Broker.KEYSTORE</code>. The
 * Certificate for clients is stored in
 * <code>NB_HOME/keystore/Broker.TRUSTSTORE</code><br>
 * Use this class to regenerate keys. Prefer using the default provided with NB
 * distribution
 * 
 * @author Harshawardhan Gadgil (hgadgil@grids.ucs.indiana.edu)<br>
 *         Created: Mar 21, 2006<br>
 */
public class BrokerSecureKeysGenerator {

	static Logger log = Logger.getLogger("BrokerSecureKeysGenerator");

	public static void main(String[] args) {

		// MUST initialize the system .This correctly sets paths and other init
		// data
		SystemInit.init();

		String nbHome = null;

		try {
			nbHome = SystemInit.getInitData(SystemInit.SYS_NB_HOME);
		}
		catch (SystemNotInitializedException e) {
			e.printStackTrace();
			log.fatal("System was not initalized correctly");
			System.exit(-1);
		}

		String keyStorePass = "passpass";
		try {
			keyStorePass = SystemInit
					.getInitData(SystemInit.SYS_BROKER_KEYSTORE_PASSWORD);
		}
		catch (SystemNotInitializedException e) {
			log.warn("Using default KeyStorePassword", e);
		}

		String trustStorePass = "passpass";
		try {
			trustStorePass = SystemInit
					.getInitData(SystemInit.SYS_BROKER_TRUSTSTORE_PASSWORD);
		}
		catch (SystemNotInitializedException e) {
			log.warn("Using default TrustStorePassword", e);
		}

		// Initialize the Certificate Manager

		Properties certStoreProps = new Properties();
		certStoreProps.put("KEYSTORE_PASSWORD", keyStorePass);
		certStoreProps.put("KEYSTORE", nbHome + "/keystore/Broker.KEYSTORE");

		CertificateManager brokerKeystoreCertMan = new CertificateManager(
				certStoreProps);

		String brokerAlias = "nbbroker";

		// Step 1: Generate a self signed certificate

		KeyToolFacade.GenerateKey(brokerKeystoreCertMan, brokerAlias,
				"\"CN=NBBroker,OU=Community Grids Lab,"
						+ "O=Indiana University,L=Bloomington,S=IN,"
						+ "C=U.S.A.\"", "365");
		KeyToolFacade.GenerateSelfCertifiedCertificate(brokerKeystoreCertMan,
				brokerAlias, "\"CN=NBBroker,OU=Community Grids Lab,"
						+ "O=Indiana University,L=Bloomington,S=IN,"
						+ "C=U.S.A.\"", "365");

		// Step 2: Exprot certificate to a file

		KeyToolFacade.ExportCertificate(brokerKeystoreCertMan, brokerAlias,
				nbHome + "/keystore/Broker.cer");

		// Step 3: Import certificate to the broker truststore
		Properties trustStoreProps = new Properties();
		trustStoreProps.put("KEYSTORE_PASSWORD", trustStorePass);
		trustStoreProps.put("KEYSTORE", nbHome + "/keystore/Broker.TRUSTSTORE");

		CertificateManager trustStoreCertMan = new CertificateManager(
				trustStoreProps);
		KeyToolFacade.ImportCertificate(trustStoreCertMan, brokerAlias, nbHome
				+ "/keystore/Broker.cer");

		log.info("Certificates generated.");
		System.out.println("When using SSL connections use "
				+ "NB_HOME/keystore/Broker.TRUSTSTORE"
				+ " for Broker certificate");
	}
}
